package com.cennavi.minemap.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.bean.TokenBean;
import com.cennavi.minemap.bean.TokenSolusIdBean;
import com.cennavi.minemap.bean.TokenWithSoluBean;
import com.cennavi.minemap.dao.TokenDao;
import com.cennavi.minemap.util.DateTimeUtil;
import com.cennavi.minemap.util.HttpClientUtil;
import com.cennavi.minemap.util.InfoUtil;
import com.cennavi.minemap.util.StringUtil;

@Service
@ComponentScan(basePackages = { "com.cennavi.minemap.dao" })
@PropertySource(value = { "classpath:application.properties" })
public class TokenService {
	@Autowired
	private TokenDao tokenDao;

	@Value("${spring.minemap.token.count}")
	private int tokenCount;

	@Value("${minemap.ppmap.token.url}")
	private String ppmapTokenUrl;

	public List<TokenBean> apply(TokenBean tokenBean) {
		int count = tokenDao.queryTokenCount(tokenBean.getUserId());

		if (tokenCount == 0 || count < tokenCount) {
			String id = StringUtil.uuid().replace("-", "");
			tokenBean.setId(id);
			tokenBean.setCreateTime(DateTimeUtil.getNow());
			tokenDao.insert(tokenBean);
			return Arrays.asList(tokenDao.queryById(id));
		} else {
			return null;
		}
	}

	public int update(TokenBean tokenBean) {
		return tokenDao.update(tokenBean);
	}

	public List<TokenBean> queryByUserId(int userId) {
		return tokenDao.queryByUserId(userId);
	}

	public List<TokenBean> queryByUserIdOfNoSolu(int userId) {
		if (ppmapTokenUrl != null && !ppmapTokenUrl.equals("")) {
			String tokenId = null;
			try {

				String host = InfoUtil.getHost();
				String resp = null;
				try {
					resp = HttpClientUtil.doGet(ppmapTokenUrl + host);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (resp != null) {
					JSONObject json = JSONObject.parseObject(resp);
					if (json.getInteger("errcode") == 0) {
						tokenId = json.getString("data");
					}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
			if (tokenId != null) {
				TokenBean tokenBean = new TokenBean();
				tokenBean.setId(tokenId);
				tokenBean.setName("web_token");
				tokenBean.setType(1);
				tokenBean.setUserId(userId);
				tokenBean.setCreateTime(DateTimeUtil.getNow());
				tokenDao.insert(tokenBean);
			}
		}
		return tokenDao.queryByUserIdOfNoSolu(userId);
	}

	public List<TokenBean> queryById(String id) {
		return Arrays.asList(tokenDao.queryById(id));
	}

	@Transactional
	public int insertTokenSolu(TokenSolusIdBean tokenSolusBean) {
		List<Integer> soluIds = tokenSolusBean.getSoluIds();
		List<String> tokenIds = tokenSolusBean.getTokenIds();

		if (soluIds.size() == 1) {
			tokenDao.deleteBySoluId(soluIds.get(0));
			return tokenDao.insertTokenSolu(tokenSolusBean);
		}
		if (tokenIds.size() == 1) {
			tokenDao.deleteByTokenId(tokenIds.get(0));
			return tokenDao.insertTokenSolu(tokenSolusBean);
		}
		return -1;
	}

	public List<TokenWithSoluBean> queryBySoluUserId(String option, int soluId, int userId) {
		return tokenDao.queryBySoluUserId(option, soluId, userId);
	}

	public int queryTokenSolu(String tokenId, int soluId) {
		return tokenDao.queryTokenSolu(tokenId, soluId);
	}
}
